let house_id = '' // 默认房屋id
let house_index = 0 // 默认房屋下标
Page({
  data: {
    dialogVisible: false,
    houseList: [], // 房屋列表
    empty: false, // 房屋是否是空值
  },
  onShow() {
    this.getHouseList()
  },
  // 获取房屋列表
  async getHouseList() {
    const { code, data: houseList } = await wx.http.get('/room')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      houseList,
      empty: houseList.length === 0,
    })
  },
  // 删除房屋
  async deleteHouse() {
    // 问题: 知道删除的是哪个房屋 -> id -> where ???
    // console.log(house_id)
    // console.log(house_index)
    // 发送请求
    const { code, data } = await wx.http.delete(`/room/${house_id}`)
    if (code !== 10000) return wx.utils.toast()
    this.data.houseList.splice(house_index, 1)
    console.log(this.data.houseList)
    this.setData({
      houseList: this.data.houseList,
      empty: this.data.houseList.length === 0,
    })
  },
  dialogClose(ev) {
    // console.log(ev)
    ev.detail === 'confirm' && this.deleteHouse()
  },
  swipeClose(ev) {
    const { position, instance } = ev.detail
    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      // console.log(ev)
      // 将操作房屋的id和index进行存储 / 赋值
      house_id = ev.mark.id
      house_index = ev.mark.index
      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  goDetail(ev) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + ev.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
